<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <img src="../assets/heima.png" alt="" />
          <span>黑马20期后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggleMenu" @click="isCollapse = !isCollapse">|||</div>
          <!--  default-active的值为当前激活菜单的 index时，对应的index就会高亮，而我们给index动态设置的值就是哈希值 -->
          <el-menu
            :default-active="$route.path"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
          >
            <!-- index是element自带的属性并且要求值为一个字符串，其实传item.id就行，我就是吃多了 -->
            <el-submenu
              :index="index + 1 + ''"
              v-for="(item, index) in menuList"
              :key="item.id"
            >
              <!-- 菜单标题 -->
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + childItem.path"
                v-for="childItem in item.children"
                :key="childItem.id"
              >
                <i class="el-icon-menu"></i>
                <span>{{ childItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false
    }
  },

  created() {
    this.getMenuList()
  },

  methods: {
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取菜单栏
    async getMenuList() {
      const res = await this.$axios.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error('未获取到菜单数据')
      }
      this.menuList = res.data
      // console.log(this.menuList)
    },
    handleOpen() {},
    handleClose() {}
  }
}
</script>

<style scoped lang='less'>
.home-container,
.el-container,
.el-aside {
  height: 100%;
}
.el-container {
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;
    .header-left {
      display: flex;
      align-items: center;
      span {
        margin-left: 20px;
        font-size: 25px;
        color: #fff;
      }
    }
  }
  .el-aside {
    background-color: #373d41;
    .iconfont {
      padding-right: 5px;
    }
    .el-menu {
      // 把自带边框去掉，不然会看着像缺少一部分
      border: none;
    }
    .toggleMenu {
      height: 40px;
      text-align: center;
      line-height: 40px;
      letter-spacing: 8px;
      user-select: none;
      background-color: #333744;
      color: #eaedf1;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
